﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.Mail;
using System.Collections.Generic;
using DataAccess.DataObject;
using System.Net.Configuration;

namespace EnmineKaitlin
{
    public partial class page : Common.PageBase
    {        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["btnSend"] != null)
            {
                SendMail();
            }
            if (Request["btnRegister"] != null)
            {
                Register(); 
            }
            if (!IsPostBack)
            {
                try
                {                    
                    if (Request["p"] != null && Request["p"].ToString() != "")
                    {
                        string strpost = Request.Params["p"].ToString();
                        strpost = strpost.Substring(strpost.LastIndexOf("-") + 1);
                        int PostID = int.Parse(strpost);
                        rptContentPage.DataSource = _BPost.Get(PostID);
                        rptContentPage.DataBind();
                    }
                    else
                    {
                        Response.Redirect("~/home");
                    }

                }
                catch (Exception ex)
                {
                    Response.Redirect("~/home");
                }
            }
        }
        protected void SendMail()
        {
            if (Request["txtName"] != null&&Request["txtName"].ToString()!="")
            {
                string content = "<table border='0'>";
                content += "<tr><td>Gửi từ:</td><td>" + Request.Params["txtName"].ToString() + "(" + Request.Params["txtEmail"].ToString() + ")</td></tr>";
                content += "<tr><td>Tiêu đề:</td><td>" + Request.Params["txtTitle"].ToString() + "</td></tr>";
                content += "<tr><td>Nội dung:</td><td></td></tr>";
                content += "<tr><td colspan='2'>" + Request.Params["txtContent"].ToString() + "</td></tr>";
                content += "</table>";
                string subject = "[lien-he-EnmineKaitlin]-" + Request.Params["txtTitle"].ToString();
                SmtpSection cfg = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                List<string> lstSendTo = new List<string>();
                lstSendTo.Add(ConfigurationManager.AppSettings["email_contact"].ToString());
                if (Send_Email(cfg.Network.UserName, lstSendTo, subject, content))
                {
                    ((Label)rptContentPage.Items[0].FindControl("lblNote")).Text = ConfigurationManager.AppSettings["send_mail_success"].ToString();
                }
                else
                {
                    ((Label)rptContentPage.Items[0].FindControl("lblNote")).Text = ConfigurationManager.AppSettings["send_mail_fail"].ToString();
                }
            }
        }
        protected void Register()
        {
            string content = "<table border='0'>";
            content += "<tr><td>Họ và tên:</td><td>"+Request["txtName"].ToString()+"</td></tr>";
            content += "<tr><td>Email:</td><td>"+Request["txtEmail"].ToString()+"</td></tr>";
            content += "<tr><td>Địa chỉ:</td><td>" + Request["txtAddress"].ToString() + "</td></tr>";
            content += "<tr><td>Điện thoại:</td><td>" + Request["txtPhone"].ToString() + "</td></tr>";
            content += "<tr><td>Số lô đất(mã hiệu):</td><td>" + Request["txtSolodat"].ToString() + "</td></tr>";
            content += "<tr><td>Tên dự án:</td><td>" + Request["txtProjectName"].ToString() + "</td></tr>";
            content += "<tr><td>Ghi chú:</td><td>" + Request["txtNote"].ToString() + "</td></tr>";
            content += "</table>";
            string subject = "[dang-ky-sangiaodich-EnmineKaitlin]-" + Request["txtName"].ToString();
            SmtpSection cfg = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
            List<string> lstSendTo = new List<string>();
            lstSendTo.Add(ConfigurationManager.AppSettings["email_contact"].ToString());
            if (Send_Email(cfg.Network.UserName, lstSendTo, subject, content))
            {
                ((Label)rptContentPage.Items[0].FindControl("lblNote")).Text = ConfigurationManager.AppSettings["send_mail_success"].ToString();
            }
            else
            {
                ((Label)rptContentPage.Items[0].FindControl("lblNote")).Text = ConfigurationManager.AppSettings["send_mail_fail"].ToString();
            }
        }
    }
}
